package com.atstudying.mall.order.controller;

import com.atstudying.common.utils.PageUtils;
import com.atstudying.common.utils.R;
import com.atstudying.mall.order.entity.OrderEntity;
import com.atstudying.mall.order.entity.OrderItemEntity;
import com.atstudying.mall.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 订单
 *
 * @author zlj
 * @email 2908434305@qq.com
 * @date 2020-10-04 22:37:31
 */
@RestController
@Slf4j
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping ("/test")

    public String test(){
        OrderEntity orderEntity=new OrderEntity();
        orderEntity.setOrderSn("dd");
        orderEntity.setReceiverRegion("ddd");
        OrderItemEntity orderItemEntity=new OrderItemEntity();
        orderItemEntity.setSkuQuantity(2);
        orderItemEntity.setSpuBrand("牛逼");
        for (int i=0;i<10;i++){
            if (i%2==0){
                rabbitTemplate.convertAndSend("hello-java-exchange","hello-java-queue",orderItemEntity);

            }
            rabbitTemplate.convertAndSend("hello-java-exchange","hello-java11-queue",orderEntity);

        }
        log.info("发送消息成功");
        return "ok";
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("order:order:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("order:order:info")
    public R info(@PathVariable("id") Long id){
		OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("order:order:save")
    public R save(@RequestBody OrderEntity order){
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("order:order:update")
    public R update(@RequestBody OrderEntity order){
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("order:order:delete")
    public R delete(@RequestBody Long[] ids){
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
